<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\Export;
use Request;

class ExportController extends Controller
{

    /**
     * 用户列表导出
     * @param Request $request
     */
    public function index(Request $request)
    {

        //设置表头
        $row = [[
            "id" => 'ID',
            "nickname" => '用户昵称',
            "gender_text" => '性别',
            "mobile" => '手机号',
            "addtime" => '创建时间  '
        ]];

        //数据
        $list = [
            0 => [
                "id" => '1',
                "nickname" => '张三',
                "gender_text" => '男',
                "mobile" => '18812345678',
                "addtime" => '2019-11-21  '
            ],
            2 => [
                "id" => '2',
                "nickname" => '李四',
                "gender_text" => '女',
                "mobile" => '18812349999',
                "addtime" => '2019-11-21  '
            ]
        ];
        //执行导出
        $data = $list;//要导入的数据
        $header = $row;//导出表头
        $excel = new Export($data, $header, '导出sheetName');
        $excel->setColumnWidth(['B' => 40, 'C' => 40]);
        $excel->setRowHeight([1 => 40, 2 => 50]);
        $excel->setFont(['A1:Z1265' => '宋体']);
        $excel->setFontSize(['A1:I1' => 14, 'A2:Z1265' => 10]);
        $excel->setBold(['A1:Z2' => true]);
        $excel->setBackground(['A1:A1' => '808080', 'C1:C1' => '708080']);
        $excel->setMergeCells(['A1:I1']);
        $excel->setBorders(['A2:D5' => '#000000']);
        ob_end_clean();
        ob_start();
        return Excel::download($excel, '导出文件.xlsx');
    }

}
